package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.Recruit;
import com.xmy.cultivate.entity.Station;
import com.xmy.cultivate.entity.StationNeeds;
import com.xmy.cultivate.service.IStationNeedsService;
import com.xmy.cultivate.service.IStationService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 招聘岗位表 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-01-01
 */
@RestController
@RequestMapping("/api/station")
public class StationController {
    @Autowired
    private IStationService iStationService;
    @Autowired
    IStationNeedsService iStationNeedsService;

    /**
     * 获取岗位数据
     */
    @GetMapping("/getStationList")
    public Result getStationList() {
        QueryWrapper<StationNeeds> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("deleted", 0);
        queryWrapper.eq("status", 1);
        queryWrapper.orderByAsc("sort");
        List<StationNeeds> stationList = iStationNeedsService.list(queryWrapper);
        return new Result(Code.OK, stationList, Code.GET_OK_MSG);
    }

    /**
     * 获取校区岗位数据
     * @param type 空全部，1校，2部门
     * @return
     */
    @GetMapping("/getStationForSchool")
    public Result getStationForSchool(@RequestParam("type") String type,
                                      @RequestParam(value = "subjectsId",required = false) String subjectsId) {
        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        String sqlWhereStr ="SELECT station_id FROM adms_station_needs where 1=1";
        if (!StringUtils.isBlank(type)){
            sqlWhereStr += " and type = "+type;
        }
        if (!StringUtils.isBlank(subjectsId)){
            sqlWhereStr += " and subjects_id = "+subjectsId;
        }
        queryWrapper.inSql("id", sqlWhereStr);
        queryWrapper.orderByAsc("sort");
        List<Station> stationList = iStationService.list(queryWrapper);
        return new Result(Code.OK, stationList, Code.GET_OK_MSG);
    }

    /**
     * 获取部门岗位数据
     */
    @GetMapping("/getStationForDepartment")
    public Result getStationForDepartment(@RequestParam(value = "departmentId",required = true) String departmentId) {
        if (StringUtils.isBlank(departmentId)){
            return new Result(Code.OK, null, "部门Id不能为空！");
        }
        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql("id", "SELECT station_id FROM adms_station_needs WHERE type = 2 AND department_id =" + departmentId);
        List<Station> stationList = iStationService.list(queryWrapper);
        return new Result(Code.OK, stationList, Code.GET_OK_MSG);
    }

}
